﻿#region Copyright (c) 2008 plexBB Team

/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

#endregion

using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlexBB.Controls.Admin.UserManagement;
using PlexBB.Data;

namespace PlexBB.Controls.Admin.UserManagement
{
    public abstract class AcpUserHeader : AcpUserControlBase
    {
        #region Controls

        protected IButtonControl bGo;
        protected HyperLink hBack;
        protected ListControl sMode;
        protected ITextControl tUserName;

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                // populate sMode list
                var modes = from m in Data.Modules
                            where m.ControlType == "PlexBB.Controls.Admin.AcpUsers" && m.ModuleType == ModuleType.Acp && m.Enabled
                            orderby m.Position
                            select new { m.ModuleID, Name = Lang["ACP_USER_" + m.ModuleMode.ToUpper()] };
                sMode.DataTextField = "Name";
                sMode.DataValueField = "ModuleID";
                sMode.DataSource = modes;
                sMode.DataBind();
                sMode.Text = CurrentModule.ModuleID.ToString();

                tUserName.Text = ManagedUser.UserName;
            }
            bGo.CausesValidation = false;
            bGo.Click += Redirect;
            sMode.AutoPostBack = true;
            sMode.CausesValidation = false;
            sMode.SelectedIndexChanged += Redirect;

            hBack.NavigateUrl = PlexBBContext.CreateUrl("acp", "mid", CurrentModule.ModuleID);
        }

        private void Redirect(object sender, EventArgs e)
        {
            int moduleID = int.Parse(sMode.SelectedValue);
            Response.Redirect(PlexBBContext.CreateUrl("acp", "mid", moduleID, "u", ManagedUser.UserID));
        }
    }
}